package com.jvm;


import org.springframework.util.StopWatch;

import java.util.Hashtable;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.IntStream;

/**
 * ClassName:Demo05
 * Package:com.jvm
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/10 - 14:47
 */
public class Demo05 {
    public static void main(String[] args) {
        //测试 线程安全的集合类的性能
        //写时复制技术的集合类 CopyOnWriteArrayList
        CopyOnWriteArrayList<Integer> cow = new CopyOnWriteArrayList<>();
        //1、创建统计时间的工具类对象
        StopWatch sw = new StopWatch();
        //通过cow容器并发写100W的数据到集合中
        sw.start("cow 并发写100w数据");
        //通过cow容器并发读100W的数据
        IntStream.rangeClosed(0, 1000000)//生成1000000数据
                .parallel()//并行执行
                .forEach(num -> cow.add(num));

        sw.stop();

        sw.start("cow 并发读100w数据");

        IntStream.rangeClosed(0, 1000000)//生成1000000数据
                .parallel()//并行执行
                .forEach(num -> cow.get(num));

        sw.stop();

        //打印上面的统计结果
        System.out.println(sw.prettyPrint());

    }
}
